package com.lxg.dao;

import com.lxg.pojo.Books;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.ArrayList;

@Mapper
public interface BooksDao {

    /**
     * 查询全部的书本
     * @return
     */
    @Select("select * from books where is_deleted = 0 order by times desc")
    public ArrayList<Books> getAllBooks();

    /**
     * 根据bid查询书本
     * 这里已经包含了该书本的所有信息
     * @param bid
     * @return
     */
    @Select("select * from books where bid=#{bid}")
    public Books getBookByBid(Integer bid);

    /**
     * 根据书本bid模糊查询
     * @param Bid
     * @return
     */
    @Select("select * from books where  is_deleted = 0   and Bid  like CONCAT('%',#{bid},'%')  ")
    public ArrayList<Books> getAllLikeBooksByBid(Integer Bid);

    /**
     * 根据书本名字模糊查询
     * @param name
     * @return
     */
    @Select("select * from books where  is_deleted = 0 and name like CONCAT('%',#{name},'%') ")
    public ArrayList<Books> getAllLikeBooksByName(String name);

    /**
     * 根据书本作者模糊查询
     * @param author
     * @return
     */
    @Select("select * from books where  is_deleted = 0 and author like CONCAT('%',#{author},'%')")
    public ArrayList<Books> getAllLikeBooksByAuthor(String author);

    /**
     * 添加一本图书
     * @param name
     * @param author
     * @param num
     * @param remainder
     * @param press
     * @param address
     */
    @Insert("insert into books (name,author,num,remainder,press,address) " +
            "values (#{name},#{author},#{num},#{remainder},#{press},#{address})")
    public void addBook(String name,String author,Integer num,
                        Integer remainder, String press,String address);


    /**
     * 管理员根据bid删除一本书
     * @param bid
     */
    @Update("update books set is_deleted=1 where bid=#{bid}")
    public void deleteBookByBid(Integer bid);


    /**
     * 根据bid修改一本图书信息
     * @param name
     * @param author
     * @param address
     */
    @Update("update books set name=#{name},author=#{author}," +
            "address=#{address}, num=#{name} , remainder=#{remainder},press=#{press} where bid=#{bid}")
    public void updateBook(Integer bid,String name,Integer num,Integer remainder,String author,String address,String press);


    /**
     * 修改图书库存数量
     * @param bid
     * @param num
     */
    @Update("update books set num=#{num} " +
            "where bid=#{bid}")
    public void updateBookNum(Integer bid,Integer num);


    /**
     * 修改图书已借数量
     * @param bid
     * @param remainder
     */
    @Update("update books set remainder=#{remainder} " +
            "where bid=#{bid}")
    public void updateBookRemainder(Integer bid,Integer remainder);




    /**
     * 修改图书被借阅次数
     * @param bid
     * @param times
     */
    @Update("update books set times=#{times} where bid=#{bid}")
    public void updateBookTimes(Integer bid,Integer times);


    //图书种类数量
    @Select("select count(*) from books")
    public Integer getBooksTypeNum();


    //图书总库存
    @Select("select sum(num) from books")
    public Integer getBooksNum();


    //图书已借数量
    @Select("select sum(remainder) from books")
    public Integer getBorrowedBooksNum();



}
